<template>
  <div class="sub-sort">
    <div class="sort">
      <a
        :class="{ active: sortParams.sortField === null }"
        href="javascript:;"
        @click="changeSort(null)"
        >默认排序</a
      >
      <a
        :class="{ active: sortParams.sortField === 'publishTime' }"
        href="javascript:;"
        @click="changeSort('publishTime')"
        >最新商品</a
      >
      <a
        :class="{ active: sortParams.sortField === 'orderNum' }"
        href="javascript:;"
        @click="changeSort('orderNum')"
        >最高人气</a
      >
      <a
        :class="{ active: sortParams.sortField === 'evaluateNum' }"
        href="javascript:;"
        @click="changeSort('evaluateNum')"
        >评论最多</a
      >
      <a href="javascript:;" @click="changeSort('price')">
        价格排序
        <i
          class="arrow up"
          :class="{
            active:
              sortParams.sortField === 'price' &&
              sortParams.sortMethod === 'asc'
          }"
        ></i>
        <i
          class="arrow down"
          :class="{
            active:
              sortParams.sortField === 'price' &&
              sortParams.sortMethod === 'desc'
          }"
        ></i>
      </a>
    </div>
    <div class="check">
      <RbtCheckout v-model="sortParams.inventory" @change="changeCheck"
        >仅展示有货商品</RbtCheckout
      >
      <RbtCheckout v-model="sortParams.onlyDiscount" @change="changeCheck"
        >仅展示特惠商品</RbtCheckout
      >
    </div>
  </div>
</template>

<script setup lang="ts">
interface SortType {
  inventory: boolean
  onlyDiscount: boolean
  sortField: string | null
  sortMethod: string | null
}

const emits = defineEmits<{
  (e: 'sort-change', params: SortType): void
}>()

// 排序参数
const sortParams = reactive<SortType>({
  inventory: false,
  onlyDiscount: false,
  sortField: null,
  sortMethod: null
})

// 改变排序
const changeSort = (sortField: string) => {
  if (sortField === 'price') {
    sortParams.sortField = sortField
    if (sortParams.sortMethod === null) {
      // 第一次点击 默认是降序
      sortParams.sortMethod = 'desc'
    } else {
      // 其他情况取反
      sortParams.sortMethod = sortParams.sortMethod === 'desc' ? 'asc' : 'desc'
    }
  } else {
    // 如果排序未改变停止逻辑
    if (sortParams.sortField === sortField) return
    sortParams.sortField = sortField
    sortParams.sortMethod = null
  }
  emits('sort-change', sortParams)
}

const changeCheck = () => {
  // console.log(sortParams.onlyDiscount)
  emits('sort-change', sortParams)
}
</script>

<style scoped lang="scss">
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: $rbtColor;
        border-color: $rbtColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: $rbtColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: $rbtColor;
          }
        }
      }
    }
  }
  .check {
    .rbt-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>
